﻿using System.ComponentModel;
using System.Runtime.Serialization;
using Affinity.Core.Utility;

namespace Affinity.Synthesis
{
    public abstract class NotifyPropertyChangedBase : INotifyPropertyChanged, IRaiseNotifyPropertyChanged
    {
        public NotifyPropertyChangedBase(bool raiseOnUiThread = true)
        {
            _raiseOnUiThread = raiseOnUiThread;
        }

        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void RaisePropertyChanged(string propertyName)
        {
            if (_raiseOnUiThread)
            {
                Threading.ExecuteOnUIThread(() =>
                    {
                        if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
                    });
            }
            else
            {
                if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        void IRaiseNotifyPropertyChanged.RaisePropertyChanged(string propertyName)
        {
            RaisePropertyChanged(propertyName);
        }

        private readonly bool _raiseOnUiThread;
    }
}
